﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;


namespace LPV_POS.mh_pos
{
    public partial class mh_dmnhomhanghoa : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
       
        DataTable tb;
        string[] s;
        
        object[] o;
        // select danh sach nhom hang
        private DataTable f_selectdsnhomhh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach nhom chu hang hoa
        private DataTable f_selectdsnhomchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm chủ ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // select danh sach VAT
        private DataTable f_selectdsVATchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 3 };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn thuế VAT---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // hien thi ds nhom chu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }


        // insert nhom hang
        private int f_insertnhomhang(string tennhomhang, string manhomchu, string mavat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 2, 1, tennhomhang, manhomchu, mavat };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }

        // update mot nhom hang
        private int f_updatenhomhang(string manhomhang, string tennhomhang, string manhomchu, string mavat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 3, 1, manhomhang, tennhomhang, manhomchu, mavat };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }

        // xoa nhom hh
        private int f_deletenhomhh(string manhomhang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, manhomhang };
            int ketqua = tv.SP_Update("sp_mh_dmnhomhanghoa", s, o);
            return ketqua;
        }

        // Tim nhom hang khi nhan nut tim
        private DataTable f_selectfindnhomhang(string tennhomhh, string tenhomchu, string thuevat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMCHU", "@VAT" };
            o = new object[] { 1, 4, tennhomhh, tenhomchu, thuevat };
            tb = tv.SP_SelectDataTable("sp_mh_dmnhomhanghoa", s, o);
            return tb;
        }
        // tim thue VAT
        private DataTable f_findnhomhh()
        {
            DataTable tb;
            string tennhomhh;
            string tenhomchu;
            string thuevat;
            if (com_nhomchu.Text != "--- Chọn nhóm chủ ---")
                tenhomchu = com_nhomchu._GetselectedID();
            else
                tenhomchu = "";

            if (com_thueVAT.Text != "--- Chọn thuế VAT---")
                thuevat = com_thueVAT._GetselectedID();
            else
                thuevat = "";
       

            if (txt_tennhomhh.Text != "")
                tennhomhh = "'%" + txt_tennhomhh.Text + "%'";
            else
                tennhomhh = "";

            tb = f_selectfindnhomhang(tennhomhh, tenhomchu, thuevat);
            return tb;

        }


        public mh_dmnhomhanghoa()
        {
            InitializeComponent();
            f_showdataoncombox(com_nhomchu, f_selectdsnhomchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_thueVAT, f_selectdsVATchuforcom(), "OBJID", "OBJNAME");
        }

        private void mh_dmnhomhanghoa_Load(object sender, EventArgs e)
        {
            luoi_nhomhh.MainView = grid_nhomhh;
            luoi_nhomhh.DataSource = f_selectdsnhomhh();
            bton_tim.Text = "CLR";
            //  com_nhomchu._SetselectedID("1");
            //com_thueVAT._SetselectedID("1");
            bton_luu.Enabled = false;
            //bton_giatrithuVAT.Enabled = false;
            //bton_tenthueVAT.Enabled = false;
        }

        private void grid_nhomhh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_nhomhh.FocusedColumn;
            txt_tennhomhh.Text = grid_nhomhh.GetRowCellDisplayText(chiso, col_tennhomhh) + "";
            string tenthuevat = grid_nhomhh.GetRowCellDisplayText(chiso, col_vat) + "";
            com_thueVAT.Text = tenthuevat;
            string tennhomchu = grid_nhomhh.GetRowCellDisplayText(chiso, col_tennhomchu) + "";
            com_nhomchu.Text = tennhomchu;


            bton_tennhomhh.Enabled = false;
            bton_luu.Enabled = false;
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tennhomhh.Text = "";
            com_nhomchu._SetselectedID("1");
            com_thueVAT._SetselectedID("1");
            bton_tennhomhh.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_tennhomhh.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_nhomhh.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string manhomhang = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletenhomhh(manhomhang);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_nhomhh.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_nhomhh.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }



        private void bton_thoat_Click(object sender, EventArgs e)
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();
            }

        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            if (txt_tennhomhh.Text == "" || com_nhomchu.Text == "--- Chọn nhóm chủ ---")
                return;
            string tennhomhh = txt_tennhomhh.Text;
            string tennhomchu = com_nhomchu._GetselectedID();
            string thueVAT = "";
            if (com_thueVAT.Text == "--- Chọn thuế VAT---")
                thueVAT = "";
            else
                thueVAT = com_thueVAT._GetselectedID();

            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertnhomhang(tennhomhh, tennhomchu, thueVAT);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string manhomhang = grid_nhomhh.GetRowCellValue(grid_nhomhh.FocusedRowHandle, col_manhomhh).ToString();
                ketqua = f_updatenhomhang(manhomhang, tennhomhh, tennhomchu, thueVAT);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_donvitinh.DataSource = f_selectdonvitinh();
            }
            luoi_nhomhh.DataSource = null;
            luoi_nhomhh.DataSource = f_selectdsnhomhh();
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            if (bton_tim.Text == "CLR")
            {
                com_nhomchu.Text = "--- Chọn nhóm chủ --";
                com_thueVAT.Text = "--- Chọn thuế VAT---";
                txt_tennhomhh.Text = "";
                bton_tim.Text = "Tìm";
            }
            else
            {
                luoi_nhomhh.DataSource = null;
                luoi_nhomhh.DataSource = f_findnhomhh();
                bton_tim.Text = "CLR";
            }
        }

        private void bton_tennhomhh_Click(object sender, EventArgs e)
        {
            mh_chu frm_chu = new mh_chu();
            frm_chu.ControlBox = false;

            Variable.str_position_frmchu = "mhdmnhomhhc_bton_tennhomhh";
            frm_chu.ShowDialog();
            txt_tennhomhh.Text = Variable.str_chu;
        }
    }
}